package com.concurrent.schedule;

import com.concurrent.util.IpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scripting.support.ResourceScriptSource;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangjiawen on 2019/8/13.
 */
@Component
public class LuaRedisSchedulePlus {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private RedisTemplate redisTemplate;
    private static final String publicKey="DEMO_COMMON_KEY";
    Boolean lock=false;
    private DefaultRedisScript<String> lockScript;

    @Scheduled(cron="*/5 * * * * *")
    public void setNx() {

        try {
            String ipConfig=IpUtil.getIp() + ":" + IpUtil.getPort();
            lock=  luaExpress(publicKey,"30",ipConfig);
            if(!lock){
                String value=(String) redisTemplate.opsForValue().get(publicKey);
                logger.warn("key have exist belong to:"+value);
            }else{
                //获取锁成功
                logger.info("start lock lockNxExJob success");
               // Thread.sleep(5000);
            }
        }catch (Exception e){
            logger.error("setNx error!");
            e.printStackTrace();
        }finally {
           // redisTemplate.delete(publicKey);
        }
    }


    /**
     * 获取lua结果
     * @param key
     * @param value
     * @return
     */
    public Boolean luaExpress(String key,String  time,String value) {
        lockScript = new DefaultRedisScript<String>();
        lockScript.setScriptSource(
                new ResourceScriptSource(new ClassPathResource("add.lua")));
        lockScript.setResultType(String.class);
        // 封装参数
        List<Object> keyList = new ArrayList<Object>();
        keyList.add(key);
        keyList.add(time);
        keyList.add(value);
        String result= (String)redisTemplate.execute(lockScript, keyList);
        System.out.println(result);
        logger.info("redis set result："+result);
        if (!"ok".equals(result.toLowerCase())){
            return false;
        }
        return true;
    }
}
